// MonitorTypes.ts - Web Monitor SDK 核心类型定义
// @/utils/monitor/MonitorTypes.ts

/**
 * 监控配置接口
 */
export interface MonitorConfig {
  appId: string;                    // 应用唯一标识
  appVersion: string;               // 应用版本
  endpoint: string;                 // 上报地址
  enableAutoTrack: boolean;         // 是否自动采集
  sampleRate: number;              // 采样率 (0-1)
  maxBatchSize: number;            // 批量上报大小
  flushInterval: number;           // 上报间隔(ms)
  maxRetries: number;              // 最大重试次数
  enableDebug: boolean;            // 调试模式
  userId?: string;                 // 用户标识
  metadata?: Record<string, any>;  // 自定义元数据
}

/**
 * 监控事件接口
 */
export interface MonitorEvent {
  type: EventType;
  timestamp: number;
  sessionId: string;
  userId?: string;
  data: any;
  context?: EventContext;
}

/**
 * 事件类型枚举
 */
export enum EventType {
  ERROR = 'error',
  API = 'api',
  PERFORMANCE = 'performance',
  BEHAVIOR = 'behavior',
  CUSTOM = 'custom'
}

/**
 * 事件上下文接口
 */
export interface EventContext {
  url: string;
  userAgent: string;
  viewport: {
    width: number;
    height: number;
  };
  screen: {
    width: number;
    height: number;
  };
  connection?: {
    type: string;
    downlink?: number;
    rtt?: number;
  };
}

/**
 * 错误事件数据接口
 */
export interface ErrorEventData {
  message: string;
  stack?: string;
  filename?: string;
  lineno?: number;
  colno?: number;
  source?: 'js' | 'promise' | 'resource' | 'vue';
  componentName?: string;
  propsData?: any;
}

/**
 * API事件数据接口
 */
export interface ApiEventData {
  url: string;
  method: string;
  status: number;
  duration: number;
  requestSize?: number;
  responseSize?: number;
  success: boolean;
  errorMessage?: string;
}

/**
 * 性能事件数据接口
 */
export interface PerformanceEventData {
  type: 'navigation' | 'resource' | 'paint' | 'measure';
  name: string;
  duration: number;
  startTime: number;
  entryType: string;
  initiatorType?: string;
  transferSize?: number;
  encodedBodySize?: number;
  decodedBodySize?: number;
}

/**
 * 用户行为事件数据接口
 */
export interface BehaviorEventData {
  type: 'click' | 'scroll' | 'input' | 'focus' | 'blur' | 'page_view' | 'page_leave';
  target?: string;
  value?: any;
  position?: {
    x: number;
    y: number;
  };
  path?: string;
  duration?: number;
}

/**
 * 自定义事件数据接口
 */
export interface CustomEventData {
  name: string;
  value?: any;
  tags?: Record<string, string>;
  metrics?: Record<string, number>;
}

/**
 * 批量上报数据接口
 */
export interface BatchData {
  events: MonitorEvent[];
  timestamp: number;
  sessionId: string;
  deviceInfo: DeviceInfo;
}

/**
 * 设备信息接口
 */
export interface DeviceInfo {
  platform: string;
  os: string;
  osVersion: string;
  browser: string;
  browserVersion: string;
  deviceType: 'mobile' | 'tablet' | 'desktop';
  language: string;
  timezone: string;
  screenResolution: string;
  colorDepth: number;
  pixelRatio: number;
}

/**
 * 监控实例接口
 */
export interface IMonitor {
  init(config: MonitorConfig): void;
  track(event: Partial<MonitorEvent>): void;
  trackError(error: Error, extra?: any): void;
  trackApi(data: ApiEventData): void;
  trackPerformance(data: PerformanceEventData): void;
  trackBehavior(data: BehaviorEventData): void;
  trackCustom(data: CustomEventData): void;
  setUserId(userId: string): void;
  setMetadata(metadata: Record<string, any>): void;
  flush(): Promise<void>;
  destroy(): void;
}

/**
 * 采集器基础接口
 */
export interface ICollector {
  name: string;
  init(config: MonitorConfig): void;
  start(): void;
  stop(): void;
  destroy(): void;
}

/**
 * 数据处理器接口
 */
export interface IProcessor {
  process(events: MonitorEvent[]): MonitorEvent[];
}

/**
 * 上报器接口
 */
export interface IReporter {
  report(data: BatchData): Promise<boolean>;
  setConfig(config: Partial<MonitorConfig>): void;
}

/**
 * 重试配置接口
 */
export interface RetryConfig {
  maxRetries: number;
  baseDelay: number;
  maxDelay: number;
  backoffFactor: number;
}

/**
 * 存储接口
 */
export interface IStorage {
  set(key: string, value: any): void;
  get(key: string): any;
  remove(key: string): void;
  clear(): void;
} 